package maskedpixel.util 
{
	import flash.display.Stage;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import net.flashpunk.FP;
	import flash.events.*;
	/**
	 * ...
	 * @author MaskedPixel
	 */
	public final class LocalFileGetter 
	{
		private var loadedFunction:Function;
		
		/**
		 * Constructor.
		 */
		function LocalFileGetter()
		{
			
		}
		
		/**
		 * 
		 * @param	location
		 * @param	loaded
		 */
		public function loadLocalFile(location:String, loaded:Function):void
		{
			loadedFunction = loaded;
			var loader:URLLoader = new URLLoader();
			configureListeners(loader);
			
			var request:URLRequest = new URLRequest(location);
			try
			{
				loader.load(request);
			}
			catch (error:Error)
			{
				trace("Unable to load the requested document foo!");
			}
		}
		
		
		private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }

        private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            loadedFunction(loader.data);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
	}

}